package com.xzw.cameratest.organization;

import com.android.business.adapter.DataAdapterImpl;
import com.android.business.adapter.DeviceWithChannelList;
import com.android.business.adapter.DeviceWithChannelListBean;
import com.android.business.entity.ChannelInfo;
import com.android.business.entity.GroupInfo;
import com.android.business.exception.BusinessException;

import java.util.ArrayList;
import java.util.List;

/**
 * @descripe:
 * @author:111144
 * @date:2020/4/27
 */
public class GroupAdapterImpl implements GroupAdapterInterface {
    static class Instance {
        static GroupAdapterImpl instance = new GroupAdapterImpl();
    }


    public static GroupAdapterImpl getInstance() {
        return Instance.instance;
    }

    @Override
    public List<GroupInfo> loadGroup(String groupId) throws BusinessException {

        List<GroupInfo> groupInfoList = DataAdapterImpl.getInstance().queryGroup(groupId);
        return groupInfoList;
    }

    @Override
    public List<ChannelInfo> loadChannelByChannelIds(List<String> channelIds) throws BusinessException {
        DeviceWithChannelList result = DataAdapterImpl.getInstance().getDeviceListByChannelList(channelIds);
        if (result == null || result.getDevWithChannelList() == null ||
                result.getDevWithChannelList().size() == 0) {
            return new ArrayList<>();
        }

        List<ChannelInfo> channelInfoList = new ArrayList<>();

        for (DeviceWithChannelListBean data : result.getDevWithChannelList()) {
            if (data.getChannelList() != null && !data.getChannelList().isEmpty()) {
                channelInfoList.addAll(data.getChannelList());
            }
        }

        return channelInfoList;
    }

    @Override
    public List<ChannelInfo> loadChannelIdByGroupId(String groupId) throws BusinessException {

        DeviceWithChannelList result = DataAdapterImpl.getInstance().queryGroupDevices(groupId, null);
        if (result == null || result.getDevWithChannelList() == null || result.getDevWithChannelList().size() == 0){
            return new ArrayList<>();
        }

        List<ChannelInfo> channelInfoList = new ArrayList<>();

        for (DeviceWithChannelListBean data : result.getDevWithChannelList()) {
            if (data.getChannelList() != null && !data.getChannelList().isEmpty()) {
                channelInfoList.addAll(data.getChannelList());
            }
        }
        return channelInfoList;
    }

}
